/// 本文件由reazhuang@outlook.com开发编写 *CodeMaid自动格式化*
using System.Net;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using WalkingTec.Mvvm.Core;

namespace ReaWTM.LayuiHelper
{
	[HtmlTargetElement("wt:textbox", Attributes = REQUIRED_ATTR_NAME, TagStructure = TagStructure.WithoutEndTag)]
	public class TextBoxTagHelper : BaseFieldTag
	{
		#region 公开属性

		/// <summary>
		/// 可清空
		/// </summary>
		public bool Clearable { get; set; } = true;
		/// <summary>
		/// 点击后自动选中内容
		/// </summary>
		public bool IsAutoSelect { get; set; }
		/// <summary>
		/// 文本框autocomplate的防抖时间
		/// </summary>
		public uint? Debounce { get; set; }


		public bool IsPassword { get; set; }

		public ModelExpression LinkField { get; set; }

		/// <summary>
		/// combobox联动字段
		/// </summary>
		/// <summary>
		/// combobox联动id
		/// </summary>
		public string LinkId { get; set; }

		public string SearchUrl { get; set; }

		/// <summary>
		/// 启用search-url时，绑定显示的字段
		/// 默认为Text
		/// </summary>
		public string TextKey { get; set; } = "Text";

		/// <summary>
		/// placeholder
		/// </summary>
		/// <summary>
		/// 远程接口,框架自动附加上?keywords=
		/// 接口类型get
		/// </summary>
		/// <summary>
		/// 触发的url，框架自动加上?id=
		/// </summary>
		public string TriggerUrl { get; set; }

		/// <summary>
		/// 自动修剪空格
		/// </summary>
		public bool Trim { get; set; }
		/// <summary>
		/// 启用search-url时，绑定值的字段
		/// 默认为Value
		/// </summary>
		public string ValueKey { get; set; } = "Value";
		#endregion

		/// <summary>
		/// 改变选择时触发的js函数，func(data)格式;
		/// data.Text得到选中文本;
		/// data.Value得到被选中的值;
		/// triggerUrl为闭包参数，可直接在ChangeFunc访问到
		/// 返回false则会阻止triggerUrl的请求
		/// </summary>
		public string ChangeFunc { get; set; }

		public override void Process(TagHelperContext context, TagHelperOutput output)
		{
			output.TagName = "input";
			output.TagMode = TagMode.StartTagOnly;
			output.Attributes.Add("type", IsPassword ? "password" : "text");

			//rea-design
			SetDebounce();
			SetDefaultValue(output);
			SetLinkTo(output);

			output.Attributes.Add("class", $"layui-input {base.GetBgColorClass()}");
			output.Attributes.Add("trim", GetBooleanString(Trim));
			output.Attributes.Add("is-auto-select", GetBooleanString(IsAutoSelect));

			output.SetAutocomplete(SearchUrl);

			context.SetItem("clearable", Clearable);

			if (string.IsNullOrEmpty(SearchUrl) == false)
			{
				output.PostElement.AppendHtml($@"<script>
wtm.textbox_Render({{
id:'{Id}',
searchUrl:'{SearchUrl}',
debounce:{Debounce},
changeFunc:'{ChangeFunc}',
valueKey:'{ValueKey}',
textKey:'{TextKey}',
triggerUrl:'{TriggerUrl}',
beforeMount:'{BeforeMount}',
mounted:'{Mounted}',
}});
</script>"); 
			}

			base.Process(context, output);
		}

		private void SetDebounce()
		{
			if (Debounce == null)
			{
				Debounce = 300;
			}
		}
		private void SetDefaultValue(TagHelperOutput output)
		{
			var value = Field?.Model?.ToString();
			if (string.IsNullOrEmpty(value))
			{
				value = DefaultValue;
			}
			output.Attributes.Add("value", WebUtility.HtmlDecode(value));
		}
		private void SetLinkTo(TagHelperOutput output)
		{
			if (LinkField != null || string.IsNullOrEmpty(LinkId) == false)
			{
				string linkto;
				if (string.IsNullOrEmpty(LinkId))
				{
					linkto = Utils.GetIdByName(LinkField.ModelExplorer.Container.ModelType.Name + "." + LinkField.Name);
				}
				else
				{
					linkto = LinkId;
				}
				output.Attributes.Add("wtm-linkto", $"{linkto}");
			}
		}
	}
}